package com.qw.flutter.nativedialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


/**
 * @date: 2021/5/13 11:43
 * @author: lixu
 * @description: native loading Dialog
 */
class LoadingDialog implements DialogInterface.OnCancelListener {
    private static final String TAG = "LoadingDialog";
    private Context activity;
    private Dialog progressDialog;
    private TextView tvMsg;
    private String tag;
    private OnDialogCancelListener cancelListener;
    private String loadingStyle;

    public LoadingDialog(Context activity) {
        super();
        this.activity = activity;
    }

    public void dismissLoadDialog() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    public void dismissLoadDialog(boolean isSetNull) {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
            if (isSetNull) {
                progressDialog = null;
            }
        }
    }

    public void showLoadDialog(@Nullable String tag, String message, boolean isCanCancel,
                               @NonNull String loadingStyle, OnDialogCancelListener cancelListener) {
        if (activity == null) {
            return;
        }

        this.tag = tag;
        this.cancelListener = cancelListener;
        if (progressDialog == null) {
            progressDialog = new Dialog(activity, R.style.loading_dialog_progress_style);
            progressDialog.setContentView(R.layout.layout_loading_dialog);
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            progressDialog.getWindow().setWindowAnimations(R.style.loading_dialog_anim_style);

            tvMsg = (TextView) progressDialog.findViewById(R.id.id_tv_loading_msg);
        }
        if (tag != null) {
            progressDialog.setOnCancelListener(this);
        }
        progressDialog.setCancelable(isCanCancel);
        if (!loadingStyle.equals(this.loadingStyle)) {
            this.loadingStyle = loadingStyle;
        }

        if (TextUtils.isEmpty(message)) {
            tvMsg.setText("");
            tvMsg.setVisibility(View.GONE);
        } else {
            tvMsg.setText(message);
            tvMsg.setVisibility(View.VISIBLE);
        }
        progressDialog.show();

    }

    /**
     * 弹窗是否正在显示中
     *
     * @return
     */
    public boolean isShowing() {
        return progressDialog != null && progressDialog.isShowing();
    }

    /**
     * 通过物理返回按钮关闭弹窗时，回调该方法
     *
     * @param dialog
     */
    @Override
    public void onCancel(DialogInterface dialog) {
        if (cancelListener != null) {
            cancelListener.onCancel(tag);
            tag = null;
        }
    }
}
